<template>
    <!-- pages/forum/forum.wxml -->
    <view class="containView">
        <block v-for="(item, index) in catlist" :key="index">
            <view class="cat">
                <rich-text :nodes="item.name"></rich-text>
            </view>

            <view class="forum_section">
                <view class="forum_cell bar_right" @tap="toFourmdisplay" :id="forum.fid" v-for="(forum, index1) in item.forums" :key="index1">
                    <rich-text :nodes="forum.name"></rich-text>

                    <text>({{ forum.posts }})</text>
                </view>
            </view>
        </block>
    </view>
</template>

<script>
// pages/forum/forum.js
const forumindexUrl = require('../../config').forumindexUrl;

const minImgDoc = require('../../config').minImgDoc;

const app = getApp();

export default {
    data() {
        return {
            catlist: '',

            forum: {
                fid: '',
                name: '',
                todayposts: ''
            }
        };
    },
    onLoad: function (options) {
        uni.showLoading();
        this.forumIndexRequest();
    },
    onShow: function (options) {
        uni.setNavigationBarTitle({
            title: '版块'
        });
    },
    onPullDownRefresh: function () {
        this.forumIndexRequest();
    },
    methods: {
        forumIndexRequest() {
            app.globalData.apimanager
                .getRequest(forumindexUrl)
                .then((res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    var catlist = res.Variables.catlist;
                    var forumlist = res.Variables.forumlist;
                    forumlist.forEach(function (data) {
                        data.name = data.name.substring(0, 20);
                    });

                    for (let i = 0; i < catlist.length; i++) {
                        catlist[i].name = catlist[i].name.substring(0, 20);
                        let forums = catlist[i].forums;

                        for (let j = 0; j < forums.length; j++) {
                            let fid = forums[j];

                            for (let k = 0; k < forumlist.length; k++) {
                                let info = forumlist[k];

                                if (fid == info.fid) {
                                    forums[j] = info;
                                    break;
                                }
                            }
                        }
                    }

                    this.catlist= catlist;
                })
                .catch((res) => {
                    uni.stopPullDownRefresh();
                    uni.hideLoading();
                });
        },

        toFourmdisplay(e) {
            var fid = e.currentTarget.id;
            uni.navigateTo({
                url: '../forumdisplay/forumdisplay?fid=' + fid
            });
        }
    }
};
</script>
<style>
@import './forum.css';
</style>
